package org.jrfe.gfx;

import java.nio.ShortBuffer;

import org.jrfe.util.BufferUtil;
import org.lwjgl.BufferUtils;

/**
 * Vertex Batch thats also holds index buffer
 * @author Kalvis Freimanis
 *
 */
public class IdxVertexBatch extends VertexBatch {

	/** Index buffer */
	private ShortBuffer indexes;
	
	/** Index Count */
	private int idxCount;
	
	/**
	 * Create new IdxVertexBatch
	 * @param elements vertex count
	 * @param idxCount index count
	 */
	public IdxVertexBatch(int elements,int idxCount) {
		super(elements);
		indexes = BufferUtils.createShortBuffer(idxCount);
		
	}
	
	/**
	 * Resize this batches buffers
	 * Resize has same roles as {@link VertexBatch}.resize()
	 * @param newElementCount new Element count
	 * @param newIdxCount new index count
	 */
	public void resize(int newElementCount, int newIdxCount) {
		super.resize(newElementCount);
	
		if(idxCount == newIdxCount){
			return;
		}
		
		if(idxCount < newIdxCount){
			
			indexes = BufferUtil.copyBuffer(newIdxCount, indexes);
	
		}else{
			indexes = BufferUtils.createShortBuffer(idxCount);
		}
		
		idxCount = newIdxCount;
		
	}
	
	/**
	 * Update index value
	 * @param idx Index of Index in buffer
	 * @param value to set in index
	 */
	public void updateIndex(int idx,short value){
		indexes.put(idx, value);
	}
	
	/**
	 * Get Index Buffer
	 * @return Index Buffer
	 */
	public ShortBuffer getIndexes() {
		return indexes;
	}
	
	/**
	 * Get index count
	 * @return index count
	 */
	public int getIdxCount() {
		return idxCount;
	}
	
}
